﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace QDT.Core.Projects
{
    public class RepaymentScheme {
        private readonly IList<RepaymentStage> _items;

        public RepaymentScheme(List<RepaymentStage> stages) {
            stages.Sort();

            _items = stages.AsReadOnly();

            Init();
        }

        private void Init() {

            foreach (var stage in Stages)
            {
                //总收益
                TotalEarnings += stage.Interest;

                if (!stage.IsRepay)
                {
                    //剩余收益
                    WaitRepayInterest += stage.Interest;
                    //剩余本金
                    WaitRepayPrincipal += stage.Principal;
                }else {
                    //已回收利息
                    RepayInterest += stage.Interest;
                    //已回收本金
                    RepayPrincipal += stage.Principal;
                }
            }

            foreach (RepaymentStage stage in Stages)
            {
                if (stage.IsRepay) continue;
                CurrentStage = stage;
                break; 
            }

            IsCompleted=Stages.All(x => x.IsRepay);
        }

        /// <summary>
        /// Stage集合
        /// </summary>
        public IList<RepaymentStage> Stages { get { return _items; } }

        /// <summary>
        /// 第一期Stage
        /// </summary>
        public RepaymentStage FirstStage { get { return Stages[0]; } }

        /// <summary>
        /// 最后一期Stage
        /// </summary>
        public RepaymentStage LastStage { get { return Stages[Stages.Count - 1]; } }

        /// <summary>
        /// 获得当前未偿还的Stage,如果已经偿还完成所有Stage，返回null
        /// 可以先调用<see cref="IsCompleted"/>属性检查是否已经全部完成
        /// </summary>
        public RepaymentStage CurrentStage { get; private set; }

        /// <summary>
        /// 已还（回）利息
        /// </summary>
        public decimal RepayInterest { get; private set; }

        /// <summary>
        /// 已还（回）本金
        /// </summary>
        public decimal RepayPrincipal { get; private set; }

        /// <summary>
        /// 已还（回）总额
        /// </summary>
        public decimal RepayMoney{get { return RepayInterest + RepayPrincipal; }}

        /// <summary>
        /// 总收益
        /// </summary>
        public decimal TotalEarnings { get; private set; }


        /// <summary>
        /// 待还（回）的利息
        /// </summary>
        public decimal WaitRepayInterest {
            get ;
            private set;
        }

        /// <summary>
        /// 待还（回）本金
        /// </summary>
        public decimal WaitRepayPrincipal { get; private set; }

        /// <summary>
        /// 待还（回）总额
        /// </summary>
        public decimal WaitRepayMoney {
            get { return WaitRepayInterest + WaitRepayPrincipal; }
        }

        /// <summary>
        /// 检查是否全部完成
        /// </summary>
        public bool IsCompleted { get; private set; }

        public RepaymentStage this[int index] {
            get {
                var stage = _items.FirstOrDefault(x => x.Stage == index);
                return stage;
            }
        }

        /// <summary>
        /// 获得时间的所在期，如果不在，返回null
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public RepaymentStage GetIncludeStage(DateTime date) {
            var stage = Stages.FirstOrDefault(repaymentStage => repaymentStage.Time > date);
            return date>LastStage.Time ? null : stage;
        }

        public RepaymentStage GetUnRepayStage()
        {
            return Stages.FirstOrDefault(repaymentStage => !repaymentStage.IsRepay);
        }
    }
}